﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： GovLogLogin.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Repository;
using CGT.Infrastructure.Dto;
using OF.Utility;
using System.Text;
namespace CGT.Infrastructure.Service
{

    /// <summary>
    ///
    /// </summary>
    public class GovLogLoginService
    {
        protected GovLogLoginRepository repository;

        public GovLogLoginService()
        {
            repository = new GovLogLoginRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(GovLogLogin entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Insert(entity);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<GovLogLogin> entitys)
        {
            foreach (var entity in entitys)
            {
                entity.CheckCode = GetCheckCode(entity);
            }
            return repository.Insert(entitys);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(GovLogLogin entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Update(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(GovLogLogin entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogLogin SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }


        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogLogin Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<GovLogLogin> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 获取固定时间端的登陆日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public List<GovLogLogin> GetList(int userId, DateTime beginDate, DateTime endDate)
        {
            return repository.GetList(userId, beginDate, endDate);
        }

        /// <summary>
        /// 据上次登录间隔时间段内所有失败的情况
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public Page<GovLogLogin> GetLastTimeLoginFailPages(int userId, DateTime beginDate, DateTime endDate, long pageIndex, long pageSize)
        {
            var szLastLoginTime = DateTime.Parse("1970-01-01");
            if (beginDate > szLastLoginTime)
            {
                szLastLoginTime = beginDate;
            }
            return repository.GetLastTimeLoginFailPages(userId, beginDate, endDate, pageIndex, pageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="incidentType"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogLoginDto> GetList(ScLogLogin condition)
        {
            Page<GovLogLoginDto> list = repository.GetList(condition);
            foreach (var item in list.rows)
            {
                item.CurrentCheckCode = GetCheckCode(item);
            }
            return list;
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="condition"></param>
        public void ImportDataToExcel(ScLogLogin condition)
        {
            string[] caption = { "用户", "事件", "来源", "事件类型", "结果", "描述", "发生时间" };
            string[] filed = { "UserName", "Incident", "Source", "IncidentTypeName", "Result", "Description", "CreatedDate" };
            Dictionary<int, string[]> dicRows = new Dictionary<int, string[]>();
            Page<GovLogLoginDto> list = GetList(condition);
            ExcelHelper.ExportDataSetToFile<GovLogLoginDto>(caption,list.rows, filed, "登陆日志.xlsx");
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LoginStatisicDto> SearchStatisticalInformation(ScLogLogin condition)
        {
            return repository.SearchStatisticalInformation(condition);
        }

        /// <summary>
        /// 获取校验码
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string GetCheckCode(GovLogLogin entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SiteUtility.GetAppSettings("LogCheckCode"));
            sb.Append(entity.UserId);
            sb.Append(entity.Incident);
            sb.Append(entity.Source);
            sb.Append(entity.IncidentType);
            sb.Append(entity.Result);
            sb.Append(entity.Description);
            sb.Append(entity.CreatedDate.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.IncidentStatus);
            return MD5Helper.MD5Key(sb.ToString());
        }
    }

}